{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn import tree\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = np.genfromtxt(\"cart.csv\",delimiter =',')\n",
    "x_data = data[1:,1:-1]\n",
    "y_data = data[1:,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   1.,    0.,    1.,    0.,    0.,  125.],\n",
       "       [   0.,    1.,    0.,    1.,    0.,  100.],\n",
       "       [   0.,    1.,    1.,    0.,    0.,   70.],\n",
       "       [   1.,    0.,    0.,    1.,    0.,  120.],\n",
       "       [   0.,    1.,    0.,    0.,    1.,   95.],\n",
       "       [   0.,    1.,    0.,    1.,    0.,   60.],\n",
       "       [   1.,    0.,    0.,    0.,    1.,  220.],\n",
       "       [   0.,    1.,    1.,    0.,    0.,   85.],\n",
       "       [   0.,    1.,    0.,    1.,    0.,   75.],\n",
       "       [   0.,    1.,    1.,    0.,    0.,   90.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  1.,  0.,  1.])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,\n",
       "            max_features=None, max_leaf_nodes=None,\n",
       "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "            min_samples_leaf=1, min_samples_split=2,\n",
       "            min_weight_fraction_leaf=0.0, presort=False, random_state=None,\n",
       "            splitter='best')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建决策树模型\n",
    "model = tree.DecisionTreeClassifier() #默认为CART\n",
    "#输入数据建立模型\n",
    "model.fit(x_data,y_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_test:[  0.   1.   0.   0.   1.  95.]\n",
      "predict:[ 1.]\n"
     ]
    }
   ],
   "source": [
    "#测试\n",
    "x_test =x_data[4]\n",
    "print(\"x_test:\"+str(x_test))\n",
    "\n",
    "predict =model.predict(x_test.reshape(1,-1))\n",
    "print(\"predict:\"+str(predict))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
